<?php
?>
<script type="text/javascript">

function addDetail(){
	var itemId = $("#item_id").val();
	var itemDesc = $("#item").val();
	var notes = $("#notes").val();
	var amount = accounting.unformat($("#total").val());
	var qty = $("#qty").val();
	var disc = accounting.unformat($("#disc").val());
	var total = accounting.unformat($("#ttl_after_disc").html());	
	window.opener.doAddDetail(itemId,itemDesc,notes,amount,qty,disc,total);
	window.close();
}
function calculateTotal(){
	//refreshDisplayCurrency();
	//$('.currency').maskMoney("unmasked");
	//alert("test");
	total = $("#total").val();
	total = accounting.unformat(total, 0);
	qty = $("#qty").val();
	qty = accounting.unformat(qty, 0);
	disc = $("#disc").val();
	disc = accounting.unformat(disc, 0);	
	ttl = (total*qty) - disc;
	$("#ttl_after_disc").html(accounting.formatMoney(ttl));	
	//$("#tmp").val(ttl);
	//refreshDisplayCurrency();
	//$("#ttl_after_disc").html($("#tmp").val());
	
	
}

$(function(){
	$("#tabs").tabs();
	$("#item").autocomplete({
		source:<?php echo $acItem;?>,
		change:function(event,ui){
			if(ui.item == null){
				alert("Invalid Item!");
				$("#item").val("");
				$("#item").focus();
			}else{
				console.log(ui.item);
				$("#item_id").val(ui.item.id);				
				$("#total").val(accounting.formatMoney(ui.item.item_price));
				calculateTotal();
			}
		}
	});
	
	
});	
</script>
<div>
	<?php
		$labelWidth="70px"; 
	?>
	<div style="border-bottom:2px solid #000;padding-bottom:5px;margin-bottom:20px">
	<?php 
		$this->widget('application.components.widgets.ActionBar', 
			array("menuAlign" => "right", 
				'items' => array(
					array(
						'id' => "btnRefresh",
						'title' => "Tambahkan",
						'icon' => "images/action/Actions-list-add-icon.png",
						'callback'=>"addDetail"
					),					
				)
			)
		);
	?>
	</div>
	<div>
		<label class="form mandatory" style="text-align:right;width:<?php echo $labelWidth;?>" >Item :</label>
		<input type="text" id="item" name="item"/>
		<input type="hidden" id="item_id" name="item_id" style="width:20px"/>
	</div>
	<div>
		<label class="form" style="vertical-align:top;text-align:right;width:<?php echo $labelWidth;?>" >Keterangan :</label>
		<textarea name="notes" id="notes" style="width:300px"></textarea>
	</div>
	<div>
		<label class="form mandatory" style="text-align:right;width:<?php echo $labelWidth;?>" >Amount :</label>		
		<input class="currency" type="text" id="total" name="total" style="width:100px" value="0" onChange="calculateTotal()"/>
	</div>
	<div>
		<label class="form mandatory" style="text-align:right;width:<?php echo $labelWidth;?>" >Qty :</label>
		<input class="currency" type="text" id="qty" name="qty" style="width:30px" value="1" onChange="calculateTotal()"/>
	</div>
	<div>
		<label class="form" style="text-align:right;width:<?php echo $labelWidth;?>" >Disc :</label>
		<input class="currency" type="text" id="disc" name="disc" style="width:100px" value="0" onChange="calculateTotal()"/>
	</div>
	<hr>
	<div style="text-align:right">		
		<label style="text-align:right;font-weight:bold;font-size:16px;width:<?php echo $labelWidth;?>" >Total :</label>
		<label class="form" id="ttl_after_disc" name="ttl_after_disc" style="font-weight:bold;font-size:16px;text-align:right;width:150px" >0</label>
		<input class="currency" type="hidden" id="tmp"/>
	</div>
</div>